﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

using Weixin.BLL;
using Weixin.BLL.ChinaTax;
using Weixin.Entity.ChinaTax;
using Weixin.Utils;

namespace Weixin.Web
{
    public partial class Article : System.Web.UI.Page
    {
        int _TypeId = 1;
        int _Page;
        protected void Page_Load(object sender, EventArgs e)
        {
            int.TryParse(Request.QueryString["typeId"], out _TypeId);
            int.TryParse(Request.QueryString["page"], out _Page);
            typeId.Value = _TypeId.ToString();

            GetTypeData();

            if (_Page > 0)
            {
                GetData();
            }
        }

        void GetTypeData()
        {
            typeBLL bll = typeBLL.GetInstance();
            typeEntity entity = bll.GetAdminSingle(_TypeId);
            hTitle.InnerText = entity.typename;
        }

        void GetData()
        {
            int pageCount = 0;
            int recordCount = 0;
            Common common = new Common();
            string strWhere = string.Empty;
            strWhere = " and isActive=1 and CTypeId=" + _TypeId;
            DataSet ds = common.GetTablePageList("Course", "CId", "startDate desc", "*", strWhere, _Page, 10, out recordCount);
            pageCount = recordCount / 10;
            if (recordCount % 10 > 0)
            {
                pageCount = recordCount / 10 + 1;
            }
            else
            {
                pageCount = recordCount / 10;
            }
            StringBuilder strBuilder = new StringBuilder();
            StringBuilder strBuilder2 = new StringBuilder();
            strBuilder.Append("{\"pageCount\":\"" + pageCount + "\",\"items\":");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                strBuilder.Append("[");
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (strBuilder2.ToString() != "")
                    {
                        strBuilder2.Append(",");
                    }
                    strBuilder2.Append("{");
                    strBuilder2.Append("\"id\":\"" + dr["cid"] + "\",");
                    strBuilder2.Append("\"typeid\":\"" + dr["ctypeid"] + "\",");
                    strBuilder2.Append("\"title\":\"" + StringOperate.CutStr(Convert.ToString(dr["title"]), 33) + "\",");
                    strBuilder2.Append("\"date\":\"" + Convert.ToDateTime(dr["startDate"]).ToString("yyyy-MM-dd") + "\",");
                    strBuilder2.Append("\"province\":\"" + dr["province"] + "\",");
                    strBuilder2.Append("\"city\":\"" + dr["city"] + "\",");
                    strBuilder2.Append("\"addr\":\"" + dr["address"] + "\"");
                    strBuilder2.Append("}");
                }
                strBuilder.Append(strBuilder2.ToString());
                strBuilder.Append("]");
            }
            strBuilder.Append("}");

            Response.Write(strBuilder.ToString());
            Response.End();
        }
    }
}